package com.baidu.structure.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.baidu.structure.bean.ContactBean;
import com.baidu.structure.bean.InviteInfoBean;
import com.baidu.structure.constants.Tables;
import com.baidu.structure.utils.LogUtils;

import java.util.ArrayList;

/**
 * Created by sunmo on 2016/12/2.
 */
public class DBManager {
    private static DBManager mDBManager;
    private static DBHelper mDBHelper;
    private static Context mContext;

    private DBManager() {
    }

    public static DBManager getInstance(Context context) {
        if (mDBManager == null) {
            mDBManager = new DBManager();
            mDBHelper = DBHelper.getInstance(context);
            mContext = context;
        }
        return mDBManager;
    }

    //insert multi contacts
    public void insertContacts(ArrayList<ContactBean> objs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        for (ContactBean b : objs) {
            insertContact(db, b);
        }
    }

    //insert contact
    private void insertContact(SQLiteDatabase db, ContactBean b) {
        String sql = "INSERT INTO " + Tables.Person._TABLE_NAME + "(" + Tables.Person._USER_ID + "," + Tables.Person._AVATAR + ")" +
                "VALUES(?,?)";
        db.execSQL(sql, new String[]{b.getName(), String.valueOf(b.getIcon())});
    }

    //insert a single obj
    public void insertContact(ContactBean b) {
        if (b == null) return;
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        insertContact(db, b);
    }

    //insert multi invite info
    public void insertInviteInfos(ArrayList<InviteInfoBean> objs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        for (InviteInfoBean b : objs) {
            insertInviteInfo(db, b);
        }
    }


    //insert a single info
    public void insertInviteInfo(InviteInfoBean b) {
        if (b == null) return;
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        insertInviteInfo(db, b);
        LogUtils.e("----"+"insert success");
    }

    //insert invite info
    private void insertInviteInfo(SQLiteDatabase db, InviteInfoBean b) {
        String sql = "INSERT INTO " + Tables.InviteInfo._TABLE_NAME + "(" +
                Tables.InviteInfo._NAME_FROM + "," +
                Tables.InviteInfo._AVATAR + "," +
                Tables.InviteInfo._DESC + "," +
                Tables.InviteInfo._WHEN + "," +
                Tables.InviteInfo._GROUP_NAME + "," +
                Tables.InviteInfo._GROUP_ID + "," +
                ")" +
                "VALUES(?,?,?,?,?,?)";
        db.execSQL(sql, new String[]{b.getFromName(), b.getAvatar(), b.getReason(), b.getTime(), b.getGroupName(), b.getGroupId()});
    }

    //query invite info
    public ArrayList<InviteInfoBean> queryInviteInfo() {
        ArrayList<InviteInfoBean> results = new ArrayList<>();
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        Cursor cursor = db.query(Tables.InviteInfo._TABLE_NAME, null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            InviteInfoBean b = new InviteInfoBean(
                    cursor.getString(cursor.getColumnIndex(Tables.InviteInfo._NAME_FROM)),
                    cursor.getString(cursor.getColumnIndex(Tables.InviteInfo._AVATAR)),
                    cursor.getString(cursor.getColumnIndex(Tables.InviteInfo._DESC)),
                    cursor.getString(cursor.getColumnIndex(Tables.InviteInfo._WHEN)),
                    cursor.getString(cursor.getColumnIndex(Tables.InviteInfo._GROUP_ID)),
                    cursor.getString(cursor.getColumnIndex(Tables.InviteInfo._GROUP_NAME))
            );
            results.add(b);
        }
        return results;
    }

    /**
     * delete
     */
    public void deleteInviteInfo(String userName) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        String sql = "DELETE FROM " + Tables.InviteInfo._TABLE_NAME + " WHERE " + Tables.InviteInfo._NAME_FROM + "=" + userName;
        db.execSQL(sql);
    }

    /**
     * get insert into values
     */
    private ContentValues getPersonValues(ContactBean bean) {
        ContentValues values = new ContentValues();
        values.put(Tables.Person._USER_ID, bean.getName());
        values.put(Tables.Person._AVATAR, bean.getIcon());
        return values;
    }
}
